import {Toast} from 'weaver-mobile';
import {CommonText} from '../components/governTools/comText/text'
//const getLabel = Tools.getLabel;
const commonText = new CommonText();

// 政务 公共工具类
const GovernConstant = {

	// api请求错误统一处理方法
  catchApiError: (result) => {
    return new Promise((resolve, reject) => {
      if (!result.api_status) {
        Toast.fail(result.msg || commonText.doFail);
        reject(result);
        return;
      }
      resolve(result);
    });
  },
  //截取路由最后的字符串
  getUrlChar:(char) => {
    let index = char.lastIndexOf('/')+1;
    return char.substr(index)
  },

//截取路由最后的字符串
  getUrlChar2:(char) => {
    let index = char.lastIndexOf('/')-3;
    return char.substr(index,3)
  },
  getUrlCharCount:(char) => {
    let thisCount = 0;
    var reg = /\//g;
    char.replace(reg, function (m, i) {
      thisCount++;
    })
    return thisCount
  },
  //重构造存在UPLOAD的condition=>fileid重新赋值
  setUploadfileid:(data) => {
    let items = data;
    Array.isArray(data) && data.map((d,index1)=>{
      if(d.conditionType && d.conditionType === "UPLOAD"){
        // d.datas && d.datas.map((da,index2)=>{
        //   try{
        //     if(da.acclink && da.fileid){
        //       const strList = (typeof da.acclink === "string" ) && da.acclink.split("=");
        //       let chooseFileid = '';
        //       Array.isArray(strList) && strList.map((str,index)=>{
        //         if(str && str.includes("fileid")){
        //           if(typeof strList[index+1] == Number)
        //           chooseFileid = strList[index+1];
        //         }
        //       })
        //       if(chooseFileid !== '') da.fileid = chooseFileid;
        //       d.datas[index2] = da;
        //       items[index1] = d;
        //     }else{
        //       throw "Data missing fileid!";
        //     }
        //   }catch(e){
        //     console.error(e);
        //   }
        // })
        try{
          if(d.maxFilesNumber === 0) d.maxFilesNumber = 100;
          items[index1] = d;
        }catch(err){
          console.error(err);
        }
      }
    })
    return items;
  }
}

export default GovernConstant;
